<?php
namespace Home\Controller;
use Think\Controller;
use Think\Verify;
class LoginController extends BaseController {

		//接收登陆数据并验证
		function login(){
			$verify=new Verify();
			$name=I("name");
            $pwd=I("pwd");
            $pwd=md5($pwd);
			$checkcode=I("checkcode");


			//比对验证吗
			if($verify->check("$checkcode")){
				//比对用户名和密码

			    $de=M("userbase")->where("name='{$name}' and pwd='{$pwd}'")->find();
			    if($de){
                    //记录操作到日志
                    $action_name = "登录";
                    $time = date("Y-m-d H:i:s", time());
                    parent::rizhi($action_name, $time);

				   //将用户信息存储到session
				    session("name",$name);
                    session("pwd",$pwd);
				    $uid=M("userbase")->field("uid")->where("name='".$name."'")->find();
					session("uid",$uid['uid']);
					//更新登录时间
					$data['last_login_time']=time();
					$result=M("userbase")->where("uid='".$uid."'")->save("$data");

                    //设置cookie的过期时间为一个月
                    setCookie('username',session("name"),time()+60*60*24*30,'/');
                    setCookie('userpwd',session("pwd"),time()+60*60*24*30,'/');
                    //比对成功跳转首页，并显示用户信息，将登陆、注册页隐藏
                    $this->redirect("index/index");
			}else{
                    echo "<script>";
                    echo "var hehe=confirm('用户名或密码有误，请重新输入！');";
                    echo "if(hehe==true){";
                    echo "window.location='".__APP__."/topnav/land'";
                    echo "}else{window.location='".__APP__."/topnav/land'}";
                    echo "</script>";
			}
		}else{
                echo "<script>";
                echo "var hehe=confirm('验证码有误，请重新输入！');";
                echo "if(hehe==true){";
                echo "window.location='".__APP__."/topnav/land'";
                echo "}else{window.location='".__APP__."/topnav/land'}";
                echo "</script>";
		}
		
	}	

}	